@extends('admin.common.main')
@section('cnt')
    <div class="x-nav">
        <span class="layui-breadcrumb">
            <a href="">首页</a>
            <a href="">演示</a>
            <a>
                <cite>导航元素</cite></a>
        </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
        <div class="layui-row">
            <form class="layui-form layui-col-md12 x-so">
                <input class="layui-input" placeholder="开始日" name="start" id="start">
                <input class="layui-input" placeholder="截止日" name="end" id="end">
                <input type="text" name="username" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                <button class="layui-btn" lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
            </form>
        </div>
        <xblock>
            <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
            <button class="layui-btn" onclick="x_admin_show('添加房东','{{route("admin.fangowners.create")}}')"><i class="layui-icon"></i>添加房东</button>
            <a href="{{ route('admin.fangowners.exports') }}" class="layui-btn">导出excel</a>
            <span class="x-right" style="line-height:40px">共有数据：88 条</span>
        </xblock>
        <table class="layui-table">
            <thead>
                <tr>
                    <th>
                        <div class="layui-unselect header layui-form-checkbox" lay-skin="primary"><i class="layui-icon">&#xe605;</i></div>
                    </th>
                    <th>ID</th>
                    <th>房东姓名</th>
                    <th>性别</th>
                    <th>年龄</th>
                    <th>电话</th>
                    <th>身份证号码</th>
                    <th>家庭住址</th>
                    <th>邮箱</th>
                    <th>操作</th>
            </thead>
            <tbody>
                @foreach($data as $item)
                <tr>
                    <td>
                        <div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id='{{ $item->id }}'><i class="layui-icon">&#xe605;</i></div>
                    </td>
                    <td>{{ $item->id }}</td>
                    <td>{{ $item->name }}</td>
                    <td>{{ $item->sex }}</td>
                    <td>{{ $item->age }}</td>
                    <td>{{ $item->phone }}</td>
                    <td>{{ $item->card }}</td>
                    <td>{{ $item->address }}</td>
                    <td>{{ $item->email }}</td>
                    <td class="td-manage">
                        <button class="layui-btn layui-btn-sm" onclick="x_admin_show('查看照片','{{route("admin.fangowners.show",$item)}}')">查看照片</button>
                       {!! $item->editBtn('admin.fangowners.edit') !!}
                       {!! $item->delBtn('admin.user.del') !!}
                    </td>
                </tr>
                @endforeach
            </tbody>
        </table>
        <div class="page">
            {{ $data->links() }}
        </div>
    </div>
@endsection
@section('js')
    <script>
        layui.use('laydate', function() {
            var laydate = layui.laydate;

            //执行一个laydate实例
            laydate.render({
                elem: '#start' //指定元素
            });

            //执行一个laydate实例
            laydate.render({
                elem: '#end' //指定元素
            });
        });

        /*用户-停用*/
        function member_stop(obj, id) {
            layer.confirm('确认要停用吗？', function(index) {
                if ($(obj).attr('title') == '启用') {
                    //发异步把用户状态进行更改
                    $(obj).attr('title', '停用')
                    $(obj).find('i').html('&#xe62f;');

                    $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
                    layer.msg('已停用!', {
                        icon: 5,
                        time: 1000
                    });

                } else {
                    $(obj).attr('title', '启用')
                    $(obj).find('i').html('&#xe601;');

                    $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
                    layer.msg('已启用!', {
                        icon: 5,
                        time: 1000
                    });
                }

            });
        }

        $(".del_user").click(function(){
            var url = $(this).data('url');
            var type = $(this).data('type');
            member_del($(this),url,type);
        });

        /*用户-删除*/
        function member_del(obj, url, type) {
            //生成一个 token_csrf
            const _token = "{{ csrf_token() }}";
            var type_do, msgs, msg;
            if (type == 'delete') {
                type_do = 'DELETE';
                msgs = '确定要删除吗?';
                msg = '删除成功';
            } else if (type == 'restore') {
                type_do = 'GET';
                msgs = '确定要恢复吗?';
            } else {
                type_do = 'GET';
                msgs = '确定要删除吗?';
            }
            layer.confirm(msgs, function(index) {
                //发异步删除数据
                $.ajax({
                    url,
                    data: {
                        _token
                    },
                    type: type_do,
                    dataType: 'json'
                }).then(({
                    status,
                    msg
                }) => {
                    if (status == 0) {
                        layer.msg(msg, {
                            icon: 1,
                            time: 1000
                        }, () => {
                            // $(obj).parents("tr").remove();
                            // $(obj).html("恢复");
                            // $(obj).removeClass('del').addClass('huifu');
                            history.go(0);
                        });
                    }
                });
                return false;
            });
        }



        function delAll(argument) {
            layer.confirm('确认要删除吗？', function(index) {
                //获取当前当前选中的数据的id
                var ids = tableCheck.getData();
                const _token = "{{ csrf_token() }}";
                if (ids.length > 0) {
                    //如果有选中，就进行删除操作
                    //确定删除   发送请求
                    $.ajax({
                        url: "{{ route('admin.user.delall') }}",
                        data: {
                            ids,
                            _token
                        },
                        type: 'DELETE'
                    }).then(ret => {
                        if (ret.status == 0) {
                            //代表删除成功了
                            layer.msg('删除成功', {
                                icon: 1
                            }, () => {
                                location.reload();
                            });
                        }
                    });
                }


            });
        }
        //给删除按钮绑定事件
    </script>
@endsection